package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxVndList;
import com.adk.backend.service.VndListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商列表管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/vnd-list")
public class VndListController {
    
    @Autowired
    private VndListService vndListService;
    
    /**
     * 分页查询供应商列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<LxVndList>> getVndList(
            @RequestParam(required = false) String vndName,
            @RequestParam(required = false) String shortName,
            @RequestParam(required = false) Integer type,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("vndName", vndName);
        params.put("shortName", shortName);
        params.put("type", type);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<LxVndList> result = vndListService.getVndList(params);
        return Result.success(result);
    }
    
    /**
     * 获取所有供应商（不分页）
     */
    @RequiresPermission("view")
    @GetMapping("/all")
    public Result<List<LxVndList>> getAllVndList(@RequestParam(required = false) Integer type) {
        List<LxVndList> result = vndListService.getAllVndList(type);
        return Result.success(result);
    }
    
    /**
     * 根据类型获取供应商列表
     */
    @RequiresPermission("view")
    @GetMapping("/by-type")
    public Result<List<LxVndList>> getVndListByType(@RequestParam Integer type) {
        List<LxVndList> result = vndListService.getVndListByType(type);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询供应商
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxVndList> getVndDetail(@RequestParam Integer id) {
        LxVndList vnd = vndListService.getVndById(id);
        if (vnd != null) {
            return Result.success(vnd);
        }
        return Result.error("供应商不存在");
    }
    
    /**
     * 创建供应商
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxVndList> createVnd(@RequestBody LxVndList vndList) {
        try {
            LxVndList result = vndListService.createVnd(vndList);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建供应商失败", e);
            return Result.error("创建供应商失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新供应商
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateVnd(@RequestBody LxVndList vndList) {
        try {
            boolean success = vndListService.updateVnd(vndList);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新供应商失败", e);
            return Result.error("更新供应商失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除供应商（逻辑删除）
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteVnd(@RequestParam Integer id, @RequestParam(required = false) Integer delUid) {
        try {
            // 如果没有传入delUid，可以从JWT token中获取当前用户ID
            // 这里暂时使用传入的参数
            boolean success = vndListService.deleteVnd(id, delUid);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除供应商失败", e);
            return Result.error("删除供应商失败：" + e.getMessage());
        }
    }
}

